package com.book.voice;

import android.content.Context;
import android.media.audiofx.LoudnessEnhancer;
import android.media.audiofx.Equalizer;
import android.media.AudioManager;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

public class AudioEffectManager {
    
    private static final String TAG = "AudioEffectManager";
    
    private Context context;
    private AudioManager audioManager;
    private Map<Integer, LoudnessEnhancer> enhancerMap;
    private Map<Integer, Equalizer> equalizerMap;
    
    private static final int DEFAULT_ATTENUATION = -2000;
    private int currentAttenuation = DEFAULT_ATTENUATION;
    
    public AudioEffectManager(Context context) {
        this.context = context;
        this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        this.enhancerMap = new HashMap<>();
        this.equalizerMap = new HashMap<>();
    }
    
    /**
     * 应用音频调小到指定音频会话
     */
    public void applyAttenuation(int audioSessionId) {
        try {
            LoudnessEnhancer enhancer = enhancerMap.get(audioSessionId);
            if (enhancer == null) {
                enhancer = new LoudnessEnhancer(audioSessionId);
                enhancer.setEnabled(true);
                enhancerMap.put(audioSessionId, enhancer);
                Log.d(TAG, "为音频会话 " + audioSessionId + " 创建了新的LoudnessEnhancer");
            }
            
            enhancer.setTargetGain(currentAttenuation);
            Log.d(TAG, "已应用音频调小: " + currentAttenuation + " 毫贝尔 (" + 
                  (currentAttenuation / 100.0) + " dB)");
            
            applyEqualizerAttenuation(audioSessionId);
            
        } catch (Exception e) {
            Log.e(TAG, "应用音频调小时出错: " + e.getMessage());
        }
    }
    
    /**
     * 使用Equalizer进行更精细的音频控制
     */
    private void applyEqualizerAttenuation(int audioSessionId) {
        try {
            Equalizer eq = equalizerMap.get(audioSessionId);
            if (eq == null) {
                eq = new Equalizer(0, audioSessionId);
                eq.setEnabled(true);
                equalizerMap.put(audioSessionId, eq);
                Log.d(TAG, "为音频会话 " + audioSessionId + " 创建了新的Equalizer");
            }
            
            short bands = eq.getNumberOfBands();
            
            for (short i = 0; i < bands; i++) {
                short[] levelRange = eq.getBandLevelRange();
                short minLevel = levelRange[0];
                short maxLevel = levelRange[1];
                
                short attenuatedLevel = (short) (minLevel + (maxLevel - minLevel) * 0.5f);
                eq.setBandLevel(i, attenuatedLevel);
            }
            
            Log.d(TAG, "已应用Equalizer调小到音频会话 " + audioSessionId);
            
        } catch (Exception e) {
            Log.e(TAG, "应用Equalizer调小时出错: " + e.getMessage());
        }
    }
    
    /**
     * 设置调小值
     */
    public void setAttenuation(int attenuation) {
        this.currentAttenuation = attenuation;
        Log.d(TAG, "调小值已更新为: " + attenuation + " 毫贝尔");
        
        for (LoudnessEnhancer enhancer : enhancerMap.values()) {
            try {
                enhancer.setTargetGain(attenuation);
            } catch (Exception e) {
                Log.e(TAG, "更新调小值时出错: " + e.getMessage());
            }
        }
    }
    
    /**
     * 获取当前调小值
     */
    public int getCurrentAttenuation() {
        return currentAttenuation;
    }
    
    /**
     * 获取调小值的dB表示
     */
    public double getAttenuationInDb() {
        return currentAttenuation / 100.0;
    }
    
    /**
     * 释放所有音频效果
     */
    public void releaseAllEffects() {
        for (LoudnessEnhancer enhancer : enhancerMap.values()) {
            try {
                enhancer.setEnabled(false);
                enhancer.release();
            } catch (Exception e) {
                Log.e(TAG, "释放LoudnessEnhancer时出错: " + e.getMessage());
            }
        }
        
        for (Equalizer eq : equalizerMap.values()) {
            try {
                eq.setEnabled(false);
                eq.release();
            } catch (Exception e) {
                Log.e(TAG, "释放Equalizer时出错: " + e.getMessage());
            }
        }
        
        enhancerMap.clear();
        equalizerMap.clear();
        Log.d(TAG, "已释放所有音频效果");
    }
    
    /**
     * 检查是否支持LoudnessEnhancer
     */
    public boolean isLoudnessEnhancerSupported() {
        try {
            LoudnessEnhancer testEnhancer = new LoudnessEnhancer(0);
            testEnhancer.release();
            return true;
        } catch (Exception e) {
            Log.e(TAG, "检查LoudnessEnhancer支持时出错: " + e.getMessage());
            return false;
        }
    }
    
    /**
     * 检查是否支持Equalizer
     */
    public boolean isEqualizerSupported() {
        try {
            Equalizer testEq = new Equalizer(0, 0);
            testEq.release();
            return true;
        } catch (Exception e) {
            Log.e(TAG, "检查Equalizer支持时出错: " + e.getMessage());
            return false;
        }
    }
} 